package com.wmg.controller;


import com.wf.captcha.utils.CaptchaUtil;
import com.wmg.entity.User;
import com.wmg.service.UserService;
import com.wmg.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wmg
 * @since 2021-12-22
 */
@Controller
@RequestMapping("/user")
public class UserController {
     @Autowired
     private UserService userService;

    //  首页
    @GetMapping("/")
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("index");
        return mv;
    }
//   注册
     @PostMapping("/regist")
     public String regist(User user,@RequestParam String verCode,
                          HttpServletRequest request,RedirectAttributes redirectAttributes){
        //先判断是否输入用户
         if (user != null){
             String isError= "no";
             //然后判断验证码是否正确
             if(verCode==null || "".equals(verCode)){
                 isError = "yes";
                 redirectAttributes.addFlashAttribute("isError",isError);
                 redirectAttributes.addFlashAttribute("verCode",verCode);
                 redirectAttributes.addFlashAttribute("msg","验证码不能为空");
                 return "redirect:/toRegist";//注册页面
             }else{
                 if (!CaptchaUtil.ver(verCode, request)) {
                     isError = "yes";
                     redirectAttributes.addFlashAttribute("isError",isError);
                     CaptchaUtil.clear(request);  // 清除session中的验证码
                     redirectAttributes.addFlashAttribute("verCode",verCode);
                     redirectAttributes.addFlashAttribute("msg","验证码不正确");
                     return "redirect:/toRegist";//注册页面
                 }
             }
             user.setPwd(MD5Utils.code(user.getPwd()));
             userService.save(user);
         }
         return "redirect:/toLogin";
     }
//  验证码
    @GetMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        CaptchaUtil.out(request, response);
    }
//  登录
    @PostMapping("/login")
    public String login(@RequestParam String name,
                        @RequestParam String pwd,

                        HttpSession session,
                        RedirectAttributes redirectAttributes
                       ) {
        User user = userService.checkUser(name, pwd);
        if (user != null) {
            user.setPwd(null);
            session.setAttribute("user", user);
            System.out.println("==========user=====" + user);
            return "index";
        } else {
            redirectAttributes.addFlashAttribute("message", "用户名或密码错误");
            System.out.println(user);
            return "redirect:/";
        }
    }
//  注销
    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("user");
        return "redirect:/";
    }

}

